/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package my12306;

import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractListModel;
import my12306.bean.Seats;
import my12306.table.KeyValueItem;

/**
 *
 * @author Jack
 */
public class SeatSelectDialog extends javax.swing.JDialog {

    private My12306ViewMini view;
    private List<KeyValueItem> seats;

    /**
     * Creates new form SeatSelectDialog
     */
    public SeatSelectDialog(My12306ViewMini view) {
        initComponents();
        setModal(true);
        this.view = view;
        MyListModel model = new MyListModel();
        for (KeyValueItem item : Seats.allSeatAsList()) {
            model.add(item);
        }
        jList1.setModel(model);
    }

    class MyListModel extends AbstractListModel {

        private List<KeyValueItem> data;

        public MyListModel() {
            this.data = new ArrayList<KeyValueItem>();
        }

        public MyListModel(List<KeyValueItem> data) {
            this.data = data;
        }

        public void add(KeyValueItem item) {
            data.add(item);
        }

        public int getSize() {
            return getData().size();
        }

        public Object getElementAt(int index) {
            return getData().get(index);
        }

        /**
         * @return the data
         */
        public List<KeyValueItem> getData() {
            return data;
        }

        /**
         * @param data the data to set
         */
        public void setData(List<KeyValueItem> data) {
            this.data = data;
        }
    }

    public List<KeyValueItem> showDialog() {
        setVisible(true);
        Object[] objs = jList1.getSelectedValues();
        List<KeyValueItem> result = new ArrayList<KeyValueItem>();
        for (Object obj : objs) {
            result.add((KeyValueItem) obj);
        }
        return result;
    }

    void log(String msg) {
        view.log(msg);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(my12306.My12306App.class).getContext().getResourceMap(SeatSelectDialog.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jList1.setName("jList1"); // NOI18N
        jScrollPane1.setViewportView(jList1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 320, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 308, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the seats
     */
    public List<KeyValueItem> getSeats() {
        if (seats == null) {
            seats = new ArrayList<KeyValueItem>();
        }
        return seats;
    }

    /**
     * @param seats the seats to set
     */
    public void setSeats(List<KeyValueItem> seats) {
        this.seats = seats;
    }
}
